package cn.kgc.mybatis.mybatis;

import cn.kgc.mybatis.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2023/2/3
 */
public class TestMyBatis {

    public static void main(String[] args) {

        try {

            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            System.out.println("factory = " + factory);
            SqlSession sqlSession = factory.openSession();
            // 参数1 ： sql语句的定位标识符
            System.out.println("----------根据id查询用户信息----------");
            User user = sqlSession.selectOne("cn.kgc.selectOne", 1);
            System.out.println("user = " + user);

            System.out.println("----------查询用户列表----------");
            List<Object> objects = sqlSession.selectList("cn.kgc.list");
            System.out.println("objects = " + objects);

            sqlSession.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
